﻿using System.Windows;

namespace HSS.Xaml.UI.Controls.PropertyGridTestApp
{
	public class CustomValueEditor : PropertyGridEditor
	{
		public CustomValueEditor(HSS.ComponentModel.HSSPropertyDescriptor descriptor)
			: base(descriptor)
		{
		}

		#region Properties
		private LabelTextBox Control
		{
			get
			{
				return (LabelTextBox)base.InlineEditControl;
			}
		}
		#endregion

		#region Overrides
		/// <summary>
		/// This is called prior to executing the default action allowing the
		/// editor to prepare for a new edit cycle.
		/// </summary>
		/// <remarks>
		/// This action is invoked by the user DoubleClicking the Entry Label
		/// </remarks>
		public override void PrepareExecuteDefaultAction()
		{
			this.ShouldSelectAllText = true;
		}
		/// <summary>
		/// Creates the Inline Editor control.
		/// </summary>
		/// <returns>The control as a FrameworkElement.</returns>
		protected override FrameworkElement CreateInlineEditControl()
		{
			return new LabelTextBox();
		}
		/// <summary>
		/// Gets the current value held by the control.
		/// </summary>
		/// <returns>The object.</returns>
		protected override object GetControlValue()
		{
			return this.Control.Text;
		}
		/// <summary>
		/// Updates the editor with the new value.
		/// </summary>
		/// <param name="newValue">The new value.</param>
		protected override void UpdateInlineEditControl(object newValue)
		{
			if (newValue is char)
			{
				if ((char)newValue == '\0')
					newValue = "";
			}

			var ks = newValue as string;
			if (null != ks)
				this.Control.Text = ks;
			else
				this.Control.Text = this.ValueToString(newValue);
			if (this.ShouldSelectAllText)
			{
				this.Control.SelectAll();
				this.ShouldSelectAllText = false;
			}
		}
		#endregion
	}
}
